package com.yangxk.ureport.provider;

import com.bstek.ureport.provider.report.file.FileReportProvider;
import com.yangxk.ureport.starter.UReportProperties;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.io.File;

public class FileProviderListen implements ApplicationContextAware {

    private UReportProperties uReportProperties;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        FileReportProvider provider = applicationContext.getBean(FileReportProvider.class);
        if (!uReportProperties.isSqlProvider()) {
            if (uReportProperties.getFiles() != null && !"".equals(uReportProperties.getFiles())) {
                provider.setFileStoreDir(uReportProperties.getFiles());
            } else {
                provider.setFileStoreDir(getDefaultDir());
            }
        } else {
            provider.setDisabled(true);
        }
    }

    public void setuReportProperties(UReportProperties uReportProperties) {
        this.uReportProperties = uReportProperties;
    }

    private String getDefaultDir() {
        String userHome = System.getProperty("user.home");
        String ureportFileDir = "ureportfile";
        File file = new File(userHome, ureportFileDir);
        if (!file.exists())
            file.mkdir();

        return file.getAbsolutePath();
    }
}
